//
//  NewViewController.m
//  A_HandHeldLOL
//
//  Created by Zhao YanBin on 16/1/16.
//  Copyright © 2016年 zhaoyanbin. All rights reserved.
//

#import "NewViewController.h"
#import "AFNModel.h"
#import "NewModel.h"
#import <UIImageView+WebCache.h>
#import "CarouselPicture.h"
#import "New3Model.h"
#import "NewCollectionViewCell.h"
#import "New2CollectionViewCell.h"
#import <MJRefresh.h>
#import "NewDianjiViewController.h"
#import "NewDianji2ViewController.h"
@interface NewViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,PassDelegate>
@property(nonatomic, retain)UITableView *tableView;
//轮播图
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, retain)NSMutableArray *lunboArr;
//自定义头部
@property(nonatomic, retain)UIView *header;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)NSTimer *timer; //定时器
@property(nonatomic, retain)UIPageControl *page; //页码

//导航栏scroll
@property(nonatomic, retain)UIScrollView *scrollview;
@property(nonatomic, retain)NSArray *sarr;

//轮播图label
@property(nonatomic, retain)UILabel *a1;

//collection
@property(nonatomic, retain)UICollectionView *colltion;

//定义全局变量
@property(nonatomic, assign)NSInteger i;

//接收网址数组
@property(nonatomic, retain)NSArray *New3DataArr;
@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    
    self.navigationItem.title = @"新闻资讯";
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
  
    
    //model上存得网址数组
    self.New3DataArr = [New3Model setDataArr];
    
    //轮播图上的label
    [self label];
    
    //导航scrollview
    [self createscroll];
    
    //collection
    [self createcollection];
    
}

//scollview与collectionview相互关联
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _colltion) {
        _scroll.contentOffset = CGPointMake(_colltion.contentOffset.x / self.view.frame.size.width * 70, 0);
    }
}
-(void)createcollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //cell的内容大小
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height  - 64 + 44 );
    
    //上左下右
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    //cell的间距
    layout.minimumLineSpacing = 0;

    _colltion.showsHorizontalScrollIndicator = YES;
    _colltion = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
//    _colltion.backgroundColor = [UIColor yellowColor];
    _colltion.delegate = self;
    _colltion.dataSource = self;
    
    //整页翻动
    _colltion.pagingEnabled = YES;
    [self.view addSubview:_colltion];
    [_colltion release];
    [layout release];
    
    //注册重用池
    [_colltion registerClass:[NewCollectionViewCell class] forCellWithReuseIdentifier:@"cellone"];
    [_colltion registerClass:[New2CollectionViewCell class] forCellWithReuseIdentifier:@"celltwo"];
    
}

//collection两个方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
-(void)passValue:(NSString *)string
{
    NewDianjiViewController *newdjVC = [[NewDianjiViewController alloc] init];
    UINavigationController *neVC = [[UINavigationController alloc] initWithRootViewController:newdjVC];
    newdjVC.wangzhi = string;
    [self presentViewController:neVC animated:YES completion:^{
    
    }];
}
-(void)pass2Value:(NSString *)string
{
    NewDianji2ViewController *newdjVC = [[NewDianji2ViewController alloc] init];
    UINavigationController *neVC = [[UINavigationController alloc] initWithRootViewController:newdjVC];
    newdjVC.wangzhi = string;
    [self presentViewController:neVC animated:YES completion:^{
        
    }];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        NewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellone" forIndexPath:indexPath];
        
        cell.delegate = self;
        
//        New2ViewController *new = [[New2ViewController alloc] init];
//        UITableView *t = [cell setWithUrlStr:@"http://qt.qq.com/static/pages/news/phone/c12_list_1.shtml"];
        
        cell.urlStr = @"http://qt.qq.com/static/pages/news/phone/c12_list_1.shtml";
//        [cell addSubview:t];
        
        
        return cell;
    }else{
        New2CollectionViewCell *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"celltwo" forIndexPath:indexPath];
        cell2.delegate = self;

//        New3ViewController *new2 = [[New3ViewController alloc] init];
//        UITableView *tt = [cell2 setWithStr:_New3DataArr[indexPath.row]];
//        [cell2 addSubview:tt];
        
        cell2.urlStr = self.New3DataArr[indexPath.row - 1];
        
        
        
        
        return cell2;
    }
    
}


-(void)createscroll
{
    self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    [self.view addSubview:_scrollview];
    _scrollview.backgroundColor = [UIColor grayColor];
    self.scrollview.contentSize= CGSizeMake(self.view.frame.size.width * 1.7, 44);
    [_scrollview release];
    self.sarr = [@[@"最新",@"赛事",@"娱乐",@"官方",@"攻略",@"活动",@"收藏"] mutableCopy];;
    
    for (int i = 0; i < _sarr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(90 * i , 10, 80, 30);
        button.tag = i *1000 + 1000;
        [button setTitle:_sarr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_scrollview addSubview:button];
        
               
    }
}


-(void)label
{
    self.a1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 170 , 375, 30)];
    _a1.backgroundColor = [UIColor grayColor];
    
    [_scroll addSubview:_a1];
    [_a1 release];
}

//



//-(void)createscrollview
//{
//    NSString *str = @"http://qt.qq.com/static/pages/news/phone/c13_list_1.shtml";
//    [AFNModel GETWithURL:str completion:^(id result) {
//        NSMutableArray *photoArr = [NSMutableArray array];
//        for (NSDictionary *dic in _lunboArr) {
//            [photoArr addObject:dic[@"list"]];
//        }
//        CarouselPicture *qqq = [[CarouselPicture alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) timeInterval:1 imageUrlArr:photoArr urlArr:nil];
//        [_header addSubview:qqq];
//    }];
//    
//}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
